package com.leetcode.tree.recursion;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/12/21 19:52
 */
public class SumNumbers_127 {

    private int sum = 0;

    public int sumNumbers(TreeNode root) {
        dfs(root, 0);
        return sum;
    }

    public void dfs(TreeNode root, int value) {
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null) {
            sum += value * 10 + root.val;
            return;
        }
        dfs(root.left, value * 10 + root.val);
        dfs(root.right, value * 10 + root.val);
    }

}
